<template>
  <el-dialog v-model="dialogVisible" :title="title" width="30%" :before-close="handleClose">
    <div>
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px">
        <el-form-item label="字段名称" prop="field">
          <el-input placeholder="输入字段名称" style="width: 100%" v-model="ruleForm.field" />
        </el-form-item>
        <el-form-item label="标识名" prop="name">
          <el-input placeholder="标识名" style="width: 100%" v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="字段类型" prop="form_type">
          <el-select v-model="ruleForm.form_type" style="width: 100%" placeholder="字段类型" @change="changeFormType"
            clearable filterable>
            <el-option label="单选" value="radio" />
            <el-option label="多选" value="checkbox" />
            <el-option label="下拉框" value="select" />
            <el-option label="单行文本" value="text" />
            <el-option label="多行文本" value="textarea" />
            <el-option label="图片" value="pic" />
            <el-option label="日期" value="date" />
            <el-option label="日期时间" value="datetime" />
          </el-select>
        </el-form-item>
        <el-form-item label="关联字典" prop="dict_id" v-if="
          ruleForm.form_type === 'radio' ||
          ruleForm.form_type === 'checkbox' ||
          ruleForm.form_type === 'select'
        ">
          <el-select v-model="ruleForm.dict_id" style="width: 100%" placeholder="关联字典" clearable filterable>
            <el-option v-for="item in dictionary" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="列表是否显示" prop="in_list">
          <el-radio-group v-model="ruleForm.in_list">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="表单是否启用" prop="in_form">
          <el-radio-group v-model="ruleForm.in_form">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="表单是否必填" prop="is_null">
          <el-radio-group v-model="ruleForm.is_null">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="默认值" prop="default_value">
          <el-input
            placeholder="默认值"
            style="width: 100%"
            v-model="ruleForm.default_value"
          />
        </el-form-item> -->
        <el-form-item label="字段说明">
          <el-input type="textarea" :row="2" placeholder="字段说明" style="width: 100%" v-model="ruleForm.remark" />
        </el-form-item>
        <el-form-item label="输入提示" prop="input_tips">
          <el-input placeholder="输入提示" style="width: 100%" v-model="ruleForm.input_tips" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submit"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { addCustomFields, saveApi } from "@/plugins/api/customer/setup/fifth";
import { listApi as dictionaryApi } from "@/plugins/api/system/dictionary";

let ruleForm = ref({});
let rules = {
  field: [{ required: true, message: "请输入字段名称", trigger: "blur" }],
  name: [{ required: true, message: "标识名", trigger: "change" }],
  form_type: [{ required: true, message: "字段类型", trigger: "change" }],
  dict_id: [{ required: true, message: "关联字典", trigger: "change" }],
  in_list: [{ required: true, message: "列表是否显示", trigger: "change" }],
  in_form: [{ required: true, message: "表单是否必填", trigger: "change" }],
  is_null: [{ required: true, message: "表单是否必填", trigger: "change" }],
  // input_tips: [{ required: true, message: "输入提示", trigger: "blur" }],
};
let dialogVisible = ref(false);
function handleClose(done) {
  ElMessageBox.confirm("是否确定关闭此对话框?")
    .then(() => {
      done();
    })
    .catch(() => { });
}
let title = ref("");
let ruleFormRef = ref(null);
function initFun(type, row) {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  if (type == "add") {
    title.value = "新增字段";
    ruleForm.value = {};
    ruleForm.value.in_list = 1;
    ruleForm.value.in_form = 1;
    ruleForm.value.is_null = 1;
  } else {
    title.value = "编辑字段";
    ruleForm.value = Object.assign({}, row);
  }
  getDictionary();
  dialogVisible.value = true;
}
const emit = defineEmits(["refreshList"]);
function submit() {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      if (ruleForm.value.field_id) {
        saveApi(ruleForm.value).then(() => {
          ElMessage.success("编辑成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      } else {
        addCustomFields({ ...ruleForm.value, tb_name: 'crm_opportunity' }).then((data) => {
          ElMessage.success("新增成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      }
    }
  });
}
function changeFormType() {
  ruleForm.value.dict_id = null;
}
let dictionary = ref([]);
function getDictionary() {
  dictionaryApi({}).then((data) => {
    dictionary.value = data;
  });
}

defineExpose({
  initFun,
});
</script>
<style scoped lang="scss"></style>
